package recursion;

public class Fab{//非递归斐波那契
	public static void main(String[] args) {
		System.out.println(f(-9));
	}
	
	public static long f(int index) {
		if(index < 1) {
			System.out.println("invalid parameter!");
			return -1;
		}
		
		if(index == 1 || index == 2) {
			return 1;
		}
		
		long f1 = 1L;
		long f2 = 1L;
		long f = 0;
		
		for(int i=0; i<index-2; i++) {
			f = f1 + f2;
			f1 = f2;
			f2 = f;
			
		}
		
		return f;
	}
}/*
public class fibo{//递归斐波那契
	public static void main(String args[]){
		System.out.println(fib(40));
	}
	
	public static int fib(int x){
		int total = 0;
		if(x>2)
		{ total = fib(x-2) + fib(x-1); return total; }
		else {return 1;}
	}
}
*/